{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "source": [
    "import numpy as np\r\n",
    "from scipy.optimize import least_squares\r\n",
    "import matplotlib.pyplot as plt"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "source": [
    "t = np.array([[1, 2], [3, 4]])\r\n",
    "t2 = np.array([5, 6])\r\n",
    "print(np.linalg.norm(t2-t, axis=1, ord=2))\r\n",
    "t[:, :-1]\r\n",
    "t-t2"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[5.65685425 2.82842712]\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[-4, -4],\n",
       "       [-2, -2]])"
      ]
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "source": [
    "t = np.array([79445, 20009, 21622, 13683, 24709, 28223, 11293, 22990, 16466])\r\n",
    "s = np.array([[-20000, 5000], \r\n",
    "              [1000, 3500],\r\n",
    "              [-3000, -4000],\r\n",
    "              [4000, 1000],\r\n",
    "              [-3500, 2000],\r\n",
    "              [1000, 6000], \r\n",
    "              [4000, -3000], \r\n",
    "              [-4000, -1500], \r\n",
    "              [6000, -1000]])\r\n",
    "\r\n",
    "bounds = np.array([[-3000, -3000, -5000], [3000, 3000, 5000]])\r\n",
    "\r\n",
    "def model(theta, s):\r\n",
    "    return 1 / 0.3 * np.linalg.norm(s - theta[:2], axis=1) + theta[-1] \r\n",
    "\r\n",
    "def cost(theta, t):\r\n",
    "    return t - model(theta, s)\r\n",
    "\r\n",
    "theta0 = [0, 0, 0]\r\n",
    "res = least_squares(cost, theta0, bounds=bounds, args=(t,))"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "source": [
    "print(res)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      " active_mask: array([0, 0, 0])\n",
      "        cost: 1040.9140719701359\n",
      "         fun: array([ -8.29685157,   4.73676479,  15.6428946 ,  -0.7179367 ,\n",
      "       -13.9890183 ,  -6.62382172, -26.82650503,   9.8084754 ,\n",
      "        26.26599825])\n",
      "        grad: array([-1.55912990e-05,  9.26571468e-06, -8.48309798e-07])\n",
      "         jac: array([[-3.19645675e+00,  9.45393323e-01, -1.00000003e+00],\n",
      "       [-6.51088122e-01,  3.26912760e+00, -1.00000003e+00],\n",
      "       [-2.98247135e+00, -1.48861550e+00, -1.00000003e+00],\n",
      "       [ 2.08123679e+00,  2.60375964e+00, -9.99999990e-01],\n",
      "       [-2.81039428e+00,  1.79242717e+00, -1.00000003e+00],\n",
      "       [-4.38902551e-01,  3.30431173e+00, -1.00000003e+00],\n",
      "       [ 2.67187943e+00, -1.99303083e+00, -9.99999990e-01],\n",
      "       [-3.33333174e+00,  3.27731968e-03, -1.00000003e+00],\n",
      "       [ 3.30702869e+00,  4.17938429e-01, -1.00000003e+00]])\n",
      "     message: 'Both `ftol` and `xtol` termination conditions are satisfied.'\n",
      "        nfev: 12\n",
      "        njev: 12\n",
      "  optimality: 0.01563826777313961\n",
      "      status: 4\n",
      "     success: True\n",
      "           x: array([ 1996.9875003 , -1505.89604023,  2990.22352894])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.3",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.3 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "b3ba2566441a7c06988d0923437866b63cedc61552a5af99d1f4fb67d367b25f"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}